import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [{
    path: "/",
    redirect: "/home",
  },
  {
    path: "/layout",
    name: "Layout",
    component: () => import("../views/Layout/index.vue"),
    children: [{
        path: "/home",
        name: "Home",
        component: () => import("../views/Layout/Home/index.vue"),
      },
      {
        path: "/classify",
        name: "Classify",
        component: () => import("../views/Layout/Classify/index.vue"),
      },
      {
        path: "/shopcar",
        name: "Shopcar",
        component: () => import("../views/Layout/Shopcar/index.vue"),
      },
      {
        path: "/my",
        name: "My",
        component: () => import("../views/Layout/My/index.vue"),
      },
    ],
  },
  {
    path: "/sale",
    name: "Sale",
    component: () => import("../views/Sale/index.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../component/Login"),
  },
  {
    path: "/regist",
    name: "Regist",
    component: () => import("../component/Regist"),
  },
  {
    path: "/group",
    name: "Group",
    component: () => import("../views/Group/index.vue"),
  },
  {
    path: '/seckill',
    name: 'Seckill',
    component: () => import('../views/Seckill/index.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/bargaining",
    name: "Bargaining",
    component: () => import("../views/Bargaining/index.vue"),
  },
  {
    path: "/liveSale",
    name: "LiveSale",
    component: () => import("../views/LiveSale/index.vue"),
  },
  {
    path: "/order",
    name: "Order",
    component: () => import("../views/Order/index.vue"),
  },
  {
    path: "/setUp",
    name: "SetUp",
    component: () => import("../views/SetUp/index.vue"),
  },
  {
    path: "/detail",
    name: "Detail",
    component: () => import("../views/Detail/index.vue"),
  },
  {
    path: "/detailspage",
    name: "DetailsPage",
    component: () => import("../views/DetailsPage/index.vue"),
  },
  {
    path: "/groupwork",
    name: "GroupWork",
    component: () => import("../views/GroupWork/index.vue"),
  },
  {
    path: "/payment",
    name: "Payment",
    component: () => import("../views/payment/index.vue"),
  },
  {
    path: '/invitation',
    name: 'Invitation',
    component: () => import('../views/invitation/index.vue')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search/index.vue')
  },
  {
    path: '/apprise',
    name: 'Appraise',
    component: () => import('../views/Appraise/index.vue')
  },
  {
    path: '/account',
    name: 'Account',
    component: () => import('../views/Account/index.vue')
  },
  {
    path: '/bankCard',
    name: 'BankCard',
    component: () => import('../views/BankCard/index.vue')
  },
  {
    path: '/record',
    name: 'Record',
    component: () => import('../views/Record/index.vue')
  },
  {
    path:'/recharge',
    name:'Recharge',
    component:()=>import('../views/Recharge/index.vue')
  },{
    path:'/inviteCut',
    name:'InviteCut',
    component:()=>import('../views/CutDown/InviteCut/index.vue')
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  console.log(from);
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (localStorage.getItem("token")) { // 通过vuex state获取当前的token是否存在
      next();
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next();
  }
});

export default router;